<template>
  <div class="Home">
    <el-container>
      <el-aside class="aside">
        <Menu/>
      </el-aside>
      <el-container>
        <el-header class="header" :style="{height:headerHeight+'px !important'}">
          <Header/>
        </el-header>
        <el-main class="main" :style="{height:mainHeight+'px'}">
          <div class="mainOuter" :style="{height:(mainHeight-30)+'px'}">
            <div class="mainInner">
              <keep-alive>
                <!-- 这里是会被缓存的视图组件 -->
                <router-view v-if="$route.meta.keepAlive" :height="mainHeight"/>
              </keep-alive>
              <!-- 这里是不被缓存的视图组件 -->
              <router-view v-if="!$route.meta.keepAlive" :height="mainHeight"/>
              <el-backtop target=".mainInner" :visibility-height="100" :right="30" :bottom="40"></el-backtop>
            </div>
          </div>
        </el-main>
        <el-footer v-show="persistence.footer" class="footer">
          <Footer/>
        </el-footer>
      </el-container>
    </el-container>
  </div>
</template>

<script>
  import Menu from '../components/Menu';
  import Header from '../components/Header';
  import Footer from '../components/Footer';
  import {mapState} from 'vuex';

  export default {
    name: "Home",
    data() {
      return {
        mainHeight: (document.body.clientHeight || document.body.clientHeight) + 'px',//主体内容高度
        headerHeight: 0,//顶部高度
      }
    },
    components: {
      Menu, Header, Footer
    },
    mounted() {
      const that = this;
      that.resizeHeight();
      window.addEventListener('resize', that.resizeHeight, false);
    },
    destroyed() {
      const that = this;
      window.removeEventListener('resize', that.resizeHeight, false)
    },
    methods: {
      resizeHeight(e) {
        const height = document.documentElement.clientHeight || document.body.clientHeight;
        if (this.persistence.type && this.persistence.footer) {
          this.mainHeight = height - 120;
        }
        else if (this.persistence.type && !this.persistence.footer) {
          this.mainHeight = height - 70;
        }
        else if (!this.persistence.type && this.persistence.footer) {
          this.mainHeight = height - 100;
        }
        else {
          this.mainHeight = height - 50;
        }
      }
    },
    computed: {
      ...mapState(['persistence'])
    },
    watch: {
      "persistence.type": {
        handler(val) {
          this.headerHeight = val ? 70 : 50;
          this.resizeHeight();
        },
        immediate: true
      },
      "persistence.footer"() {
        this.resizeHeight();
      }
    }
  }
</script>

<style scoped>
  .aside {
    width: auto !important;
    min-height: 100vh;
    background-color: #242834 !important;
  }

  .header {
    position: relative;
    padding: 0;
  }

  .footer {
    height: 50px !important;
  }

  .Home >>> .el-main, .el-main {
    padding: 0 !important;
  }

  .main {
    background: #F3F3F4;
  }

  .mainOuter {
    overflow: hidden;
    position: relative;
    margin: 15px 20px;
  }

  .mainInner {
    overflow-y: auto;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
  }
</style>
